package com.jdiv.extensions.samples;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;

import com.jdiv.extensions.JFnt;
import com.jdiv.extensions.JFontImage;


/**
 * @author  Joyal
 */
public class FntViewer implements ActionListener{
	
	private JDesktopPane desktop = new JDesktopPane();	
	private JInternalFrame frame1 = new JInternalFrame(); 
	private JInternalFrame frame2 = new JInternalFrame(); 
	private JInternalFrame frame3 = new JInternalFrame(); 
	/**
	 * @uml.property  name="jFnt"
	 * @uml.associationEnd  
	 */
	private JFnt jFnt=new JFnt();
	private ArrayList<JFontImage> img;

	int w=328;
	int h=354;
	
	public FntViewer(){
		ventanaP();
	}
	
	public void  ventanaP(){
	
//JDesktopPane	

desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);		

	//JFrame
    JFrame ventana = new JFrame();	

	ventana.setSize(800,600);
		ventana.setTitle("FNT");
		ventana.getContentPane().setLayout(new BorderLayout());
		ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
  //  ventana.getContentPane().setBackground(Color.WHITE);

	//Menu
    JMenuBar menubar = new JMenuBar();
    JMenu menu1 =	new JMenu("Archivos");
    JMenu menu2 =	new JMenu("Edicion");
    JMenu menu3 =	new JMenu("Ver");
    JMenu menu4 =	new JMenu("Ayuda");
    
	JMenuItem mitem1 = new JMenuItem("Abrir");
	JMenuItem mitem2 = new JMenuItem("Probar JFnt");
	    JMenuItem mitem3 = new JMenuItem("Salir");
		
		mitem1.addActionListener(this);
		mitem2.addActionListener(this);
		mitem3.addActionListener(this);
		
		menu1.add(mitem1);
	menu1.add(mitem2);
	menu1.add(mitem3);
	
	menubar.add(menu1);
	menubar.add(menu2);
	menubar.add(menu3);
	menubar.add(menu4);
	
	ventana.setJMenuBar(menubar);
    
 
ventana.getContentPane().add(BorderLayout.CENTER,desktop); 


	
	
ventana.setVisible(true);    

//desktop.add(ventana("Paleta",10,10,328,360,false,true,false,true));
//desktop.add(ventana2("Caracteres",450,10,328,354,true,true,false,true));


//   w=328,h=354;
}

public JInternalFrame ventana(String nombre,int x,int y,int w,int h,boolean resizable,boolean closable,boolean maximizable,boolean iconifiable){

frame1.setTitle(nombre);
frame1.setBounds(x, y, w, h); // x,y,w,h 
frame1.setResizable(resizable);
frame1.setClosable(closable); 
frame1.setMaximizable(maximizable); 
frame1.setIconifiable(iconifiable); 

frame1.setLayout(new FlowLayout());
frame1.getContentPane().setBackground(Color.black);
frame1.getContentPane().add(new JLabel(new ImageIcon(jFnt.getPaleta())));
frame1.setVisible(true);
    
return frame1;
}

public JInternalFrame ventana2(String nombre,int x,int y,int w,int h,boolean resizable,boolean closable,boolean maximizable,boolean iconifiable){

frame2.setTitle(nombre);
frame2.setBounds(x, y, w, h); // x,y,w,h 
frame2.setResizable(resizable);
frame2.setClosable(closable); 
frame2.setMaximizable(maximizable); 
frame2.setIconifiable(iconifiable); 

frame2.setLayout(new FlowLayout());
frame2.getContentPane().setBackground(Color.black);
for (int i=0;i<img.size();i++){
           frame2.getContentPane().add(new JLabel(new ImageIcon((BufferedImage)img.get(i).getImgFont())));
   }
frame2.setVisible(true);
return frame2;       
}

public JInternalFrame escribir(String nombre,int x,int y,int w,int h,boolean resizable,boolean closable,boolean maximizable,boolean iconifiable){

String texto=JOptionPane.showInputDialog(null,"Introduzca un Texto","Prueba de Texto",JOptionPane.PLAIN_MESSAGE);

frame3.setTitle(nombre);
frame3.setBounds(x, y, w, h); // x,y,w,h 
frame3.setResizable(resizable);
frame3.setClosable(closable); 
frame3.setMaximizable(maximizable); 
frame3.setIconifiable(iconifiable); 

frame3.setLayout(new FlowLayout());
frame3.getContentPane().setBackground(Color.black);
for (int i=0;i<texto.length();i++){
           frame3.getContentPane().add(new JLabel(new ImageIcon((BufferedImage)jFnt.getFont(texto.charAt(i)).getImgFont())));
   }
frame3.setVisible(true);
return frame3;       
}
	
public void actionPerformed(ActionEvent e){  
 	
	 if (e.getActionCommand()=="Abrir") 
	     {
		 
		JFileChooser chooser= new JFileChooser(System.getProperty("user.dir"));
		chooser.setFileFilter(new FileNameExtensionFilter("Div/Fenix Font (FNT)","jFnt"));
	
		int returnVal= chooser.showOpenDialog(null);
		if(returnVal==JFileChooser.APPROVE_OPTION){
		

	desktop.remove(frame1);
	desktop.remove(frame2);
	desktop.remove(frame3);
	frame1=new JInternalFrame();
	frame2=new JInternalFrame();
	frame3=new JInternalFrame();

	desktop.update(desktop.getGraphics());

	   
	    
	  jFnt.loadFnt(chooser.getSelectedFile().getAbsolutePath());
	 img=jFnt.getFnt();
	  desktop.add(ventana("Paleta",10,10,328,360,false,true,false,true));
	  desktop.add(ventana2("Caracteres",450,10,328,354,true,true,false,true));

	}

	}
	 if (e.getActionCommand().equals("Probar JFnt"))
	     {
	  desktop.remove(frame3);
	  frame3=new JInternalFrame();
	  desktop.update(desktop.getGraphics());

	  desktop.add(escribir("Prueba FNT",200,300,500,210,false,true,false,true));

	}
	   if (e.getActionCommand().equals("Salir"))
	     {
	     	System.exit(0);
	     }
	}
public static void main(String arg[]){
    try{
 
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

}catch(Exception e){
	
}
 FntViewer fntv=new FntViewer(); 
}
}
